/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
 * Copyright (c) 2019 HUST Dian Group
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation;
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Original Author: Pengyu Liu <eicliupengyu@gmail.com>
 */
#include "ns3/log.h"
#include "robo-ammo.h"
#include "robo-base.h"
#include "robo-common.h"
#include "robo-judge.h"

namespace ns3 {
NS_LOG_COMPONENT_DEFINE ("RoboAmmo");
NS_OBJECT_ENSURE_REGISTERED (RoboAmmo);
RoboAmmo::RoboAmmo ()
{
  AddUpdateCallback (MakeCallback (&RoboAmmo::UpdateRange, this));
  AddCollisionCallback (MakeCallback (&RoboAmmo::HandleCollision, this));
  SetSelfMask (SelfMaskAmmo);
  SetCollisionMask (gAmmoCollisionMask);
  m_updatePlot = MakeCallback (&RoboAmmo::PlotSelf, this);
}

RoboAmmo::~RoboAmmo ()
{
}

TypeId
RoboAmmo::GetTypeId (void)
{
  static TypeId tid = TypeId ("ns3::RoboAmmo")
                          .SetParent<RoboActor> ()
                          .SetGroupName ("Robo")
                          .AddConstructor<RoboAmmo> ();
  return tid;
}

TypeId
RoboAmmo::GetInstanceTypeId (void) const
{
  return GetTypeId ();
}

void
RoboAmmo::SetInitValue (FVector location, float redius, int injury, float range)
{
  SetGlobalLocation (location);
  SetRedius (redius);
  m_injury = injury;
  m_range = range;
}

void
RoboAmmo::Disable (void)
{
  m_range = -1;
  m_isDestroy = true;
  m_isDeleted = true;
}

void
RoboAmmo::UpdateRange (float delta)
{
  m_range -= delta;
  if (m_range < 0)
    {
      Disable ();
    }
}

void
RoboAmmo::HandleCollision (Ptr<RoboActor> oth)
{
  if (oth->GetCollisionType () == Collision_Type_Boundary)
    {
      Disable ();
      return;
    }
  Ptr<RoboBase> robo = DynamicCast<RoboBase> (oth);
  if (robo)
    {
      if (robo->m_uid != m_shooterUid)
        {
          RoboJudge::Get ()->IndicateShoot (m_shooterUid, robo->m_uid, m_injury);
          Disable ();
        }
    }
}

void
RoboAmmo::PlotSelf (FILE *fp)
{
  fprintf (fp, "am:%d@%.3f:%.3f\n", m_injury, m_globalLocation.m_x, m_globalLocation.m_y);
}

} // namespace ns3